package com.teufandsub.socialproximity.logic;

import java.io.Serializable;

import org.json.JSONObject;

import android.util.Log;

import com.teufandsub.socialproximity.controller.Constants;

/**
 * @author Bastien Mathias <bastienmathias@gmail.com>
 * @author Antoine Teuf <antoine.teuf@gmail.com>
 * @version 1.0
 * @since 2013-05-22
 * 
 * User class to store user informations.
 */

public class User implements Serializable,Comparable<User> {
	
	private static final long serialVersionUID = -7976589809050250424L;
	
	public static final String FIRSTNAME_NAME   = "firstname";
	public static final String SURNAME_NAME     = "surname";
	public static final String USERNAME_NAME    = "username";
	public static final String EMAIL_NAME       = "email";
	public static final String PASSWD_NAME      = "passwd";
	public static final String BIRTH_NAME       = "birth";
	public static final String SEX_NAME         = "sex";
	public static final String REGID_NAME       = "regid";
	public static final String KIND             = "user";
	public static final String SHOWREALNAME_NAME = "showRealName";
	public static final String AVAILABLE_NAME    = "available";
	public static final String LASTACTIVITY_NAME = "lastActivity";
	
	//Location :
	public static final String LATITUDE_NAME = "latitude";
	public static final String LONGITUDE_NAME = "longitude";
	
	
	public static final int    SEX_UNKNOWN    = 0;
	public static final int    SEX_MALE       = 1;
	public static final int    SEX_FEMALE     = 2;

	private String firstname, surname, username, email,password;
	private int sex;
	private boolean showRealName, available;
	
	public User(String firstname, String surname, String username, String email, int sex,
			boolean showRealName, boolean available) {
		super();
		this.firstname = firstname;
		this.surname = surname;
		this.username = username;
		this.email = email;
		this.sex = sex;
		this.showRealName = showRealName;
		this.password="";
	}
	
	public User(String pseudo){
		this("", "", pseudo, "", User.SEX_UNKNOWN, true,true);
	}
	
	public User(JSONObject object){
		try {
			this.firstname    = object.getString(FIRSTNAME_NAME);
			this.surname      = object.getString(SURNAME_NAME);
			this.username     = object.getString(USERNAME_NAME);
			this.email        = object.getString(EMAIL_NAME);
			this.sex          = object.getInt(SEX_NAME);
			this.showRealName = object.getBoolean(SHOWREALNAME_NAME);
			this.available    = object.getBoolean(AVAILABLE_NAME);
		} catch (Exception e) {
			Log.e(Constants.APP_NAME, "user by JSONObject : " + e.toString());
		}
		this.password="";
	}
	public String getFirstname() {
		return firstname;
	}
	
	public void setPassword(String password){
		this.password=password;
	}
	
	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}
	public String getSurname() {
		return surname;
	}
	public void setSurname(String surname) {
		this.surname = surname;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public int getSex() {
		return sex;
	}
	public void setSex(int sex) {
		this.sex = sex;
	}
	public boolean showRealName() {
		return showRealName;
	}
	public void setShowRealName(boolean showRealName) {
		this.showRealName = showRealName;
	}
	
	public boolean isAvailable() {
		return available;
	}

	@Override
	public boolean equals(Object o) {
		boolean returnValue = false;
		if(o instanceof User){
			User user = (User) o;
			if(user != null && user.getUsername().equals(username)){
				returnValue=true;
			}
		}
		Log.i(Constants.APP_NAME, "equals method call "+returnValue);
		return returnValue;
	}
	
	@Override
	public int compareTo(User another) {
		int returnValue=-1;
		if(another!=null){
			returnValue = another.getUsername().compareToIgnoreCase(username);
		}
		Log.i(Constants.APP_NAME, "compareTo method call " + returnValue);
		return returnValue;
	}
	
	@Override
	public int hashCode() {
		//return new HashCodeBuilder(22,42).append(this.pseudo).toHashCode();
		return 0;
	}
	
	/**
	 * Method to convert user informations to a {@link JSONObject}
	 * @return JSONObject informations as a JSONObject.
	 */
	public JSONObject toJSONObject(){
		JSONObject jsonUser = new JSONObject();
		try {
			jsonUser.put(User.USERNAME_NAME, this.username);
			jsonUser.put(User.FIRSTNAME_NAME, this.firstname);
			jsonUser.put(User.SURNAME_NAME, this.surname);
			jsonUser.put(User.EMAIL_NAME, this.email);
			jsonUser.put(User.SEX_NAME, this.sex);
			jsonUser.put(User.SHOWREALNAME_NAME,this.showRealName);
			jsonUser.put(User.AVAILABLE_NAME, this.available);
			jsonUser.put(User.PASSWD_NAME, this.password);
		} catch (Exception e) {
			Log.e(Constants.APP_NAME, "Error to convert user to jsonObject" + e.toString());
		}
		
		return jsonUser;
	}

	@Override
	public String toString() {
		return "User [firstname=" + firstname + ", surname=" + surname
				+ ", username=" + username + ", email=" + email + ", sex="
				+ sex + ", showRealName=" + showRealName + "]";
	}
	
	
}
